	var count_time_call_background_task = 0;
	var x_default, y_default;
	var left_show_default, top_show_default;
	var position_div;
	var position_div_left_origin;
	var position_div_top_origin;
	var position_div_left_old;
	var position_div_top_old;
	var delta_left;
	var delta_top;
	var array_info_tiles;
	var array_point;
	var insert_point = -1;
	var insert_segment_detail_response = -1;
	var update_segment_response = -1;
	var test_callBack = "";
	var zoom_level = 3;
	var time = -1;

	var wd_width;
	var wd_height;
	var boolean_test = false;
	var stt_click_browse_startnode = false;
	var stt_click_browse_endnode = false;
	var stt_update_nodeTo = false;
	var ajax_stt_responce;
	var data_response = null;
	var data_response_segment = null;
	var boundary_global = null;
	var current_start_point, current_end_point;
	
	function callback() {
		position_div = $("#dragContainer").position();
		var array_left = new Array(array_info_tiles.length);
		var array_top = new Array(array_info_tiles.length);
		for(var i = 0; i < array_info_tiles.length; i++) {
			array_left[i] = array_info_tiles[i].left;
			array_top[i] = array_info_tiles[i].top;
		}
		
		delta_left = -position_div.left - position_div_left_old;
		delta_top = -position_div.top - position_div_top_old;
		
		position_div_left_old = -position_div.left;
		position_div_top_old = -position_div.top;

		var bound_left = -position_div.left - 256;
		var bound_right = -position_div.left + $(window).width() + 256;
		var bound_top = -position_div.top - 256;
		var bound_bottom = -position_div.top + $(window).height() + 256;	

		var max_img_left = check_max(array_left) + 256;
		var min_img_left = check_min(array_left);
		var max_img_top = check_max(array_top) + 256;
		var min_img_top = check_min(array_top);
		
		var array_min_left = getIndex(min_img_left, array_left);
		var array_max_left = getIndex((max_img_left - 256), array_left);
		var array_min_top = getIndex(min_img_top, array_top);
		var array_max_top = getIndex((max_img_top - 256), array_top);
		
		var temp_right = bound_right + delta_left;
		var temp_left = bound_left + delta_left;
		var temp_top = bound_top + delta_top;
		var temp_bottom = bound_bottom + delta_top;
		
		// move left
		if(temp_right >= max_img_left) {
			for(var i = 0; i < array_min_left.length; i++) {
				var src_old = process_str_src(array_info_tiles[array_min_left[i]].x, array_info_tiles[array_min_left[i]].y, zoom_level);
				var xy_new = convertXY(array_info_tiles[array_min_left[i]].left, array_info_tiles[array_min_left[i]].top, array_info_tiles[array_min_left[i]].x, array_info_tiles[array_min_left[i]].y, max_img_left, array_info_tiles[array_min_left[i]].top);
//				var src_new = process_str_src(xy_new[0], xy_new[1], zoom_level);
				var temp = "img[src='" + src_old + "']";
				$(temp).remove();
//				$(temp).attr('src', src_new).css({"left": max_img_left+"px", "top": array_info_tiles[array_min_left[i]].top+"px"});
				$("#dragContainer").append(process_str(max_img_left, array_info_tiles[array_min_left[i]].top, xy_new[0], xy_new[1], zoom_level));
				array_info_tiles[array_min_left[i]].left = max_img_left;
				array_info_tiles[array_min_left[i]].top = array_info_tiles[array_min_left[i]].top;
				array_info_tiles[array_min_left[i]].x = xy_new[0];
				array_info_tiles[array_min_left[i]].y = xy_new[1];
			}
		}
		
		// move right
		if (temp_left <= min_img_left) {
			for(var i = 0; i < array_max_left.length; i++) {
				var src_old = process_str_src(array_info_tiles[array_max_left[i]].x, array_info_tiles[array_max_left[i]].y, zoom_level);
				var xy_new = convertXY(array_info_tiles[array_max_left[i]].left, array_info_tiles[array_max_left[i]].top, array_info_tiles[array_max_left[i]].x, array_info_tiles[array_max_left[i]].y, (min_img_left - 256), array_info_tiles[array_max_left[i]].top);
//				var src_new = process_str_src(xy_new[0], xy_new[1], zoom_level);
				var temp = "img[src='" + src_old + "']";
				$(temp).remove();
//				$(temp).attr('src', src_new).css({"left": (min_img_left - 256)+"px", "top": array_info_tiles[array_max_left[i]].top+"px"});
				$("#dragContainer").append(process_str((min_img_left - 256), array_info_tiles[array_max_left[i]].top, xy_new[0], xy_new[1], zoom_level));
				array_info_tiles[array_max_left[i]].left = min_img_left - 256;
				array_info_tiles[array_max_left[i]].top = array_info_tiles[array_max_left[i]].top;
				array_info_tiles[array_max_left[i]].x = xy_new[0];
				array_info_tiles[array_max_left[i]].y = xy_new[1];
			}
		}
		
		// move bottom	
		if (temp_top <= min_img_top) {
			for(var i = 0; i < array_max_top.length; i++) {
				var src_old = process_str_src(array_info_tiles[array_max_top[i]].x, array_info_tiles[array_max_top[i]].y, zoom_level);
				var xy_new = convertXY(array_info_tiles[array_max_top[i]].left, array_info_tiles[array_max_top[i]].top, array_info_tiles[array_max_top[i]].x, array_info_tiles[array_max_top[i]].y, array_info_tiles[array_max_top[i]].left, (min_img_top - 256));
//				var src_new = process_str_src(xy_new[0], xy_new[1], zoom_level);
				var temp = "img[src='" + src_old + "']";
				$(temp).remove();
//				$(temp).attr('src', src_new).css({"left": array_info_tiles[array_max_top[i]].left+"px", "top": (min_img_top - 256)+"px"});
				$("#dragContainer").append(process_str(array_info_tiles[array_max_top[i]].left, (min_img_top - 256), xy_new[0], xy_new[1], zoom_level));
				array_info_tiles[array_max_top[i]].left = array_info_tiles[array_max_top[i]].left;
				array_info_tiles[array_max_top[i]].top = min_img_top - 256;
				array_info_tiles[array_max_top[i]].x = xy_new[0];
				array_info_tiles[array_max_top[i]].y = xy_new[1];
			}
		}
		
		// move top
		if (temp_bottom >= max_img_top) {
//			$("span:first").text("temp: " + temp_bottom);
//			$("span:last").text("min_img_top: " + max_img_top);
			for(var i = 0; i < array_min_top.length; i++) {
				var src_old = process_str_src(array_info_tiles[array_min_top[i]].x, array_info_tiles[array_min_top[i]].y, zoom_level);
				var xy_new = convertXY(array_info_tiles[array_min_top[i]].left, array_info_tiles[array_min_top[i]].top, array_info_tiles[array_min_top[i]].x, array_info_tiles[array_min_top[i]].y, array_info_tiles[array_min_top[i]].left, max_img_top);
//				var src_new = process_str_src(xy_new[0], xy_new[1], zoom_level);
				var temp = "img[src='" + src_old + "']";
				$(temp).remove();
//				$(temp).attr('src', src_new).css({"left": array_info_tiles[array_min_top[i]].left+"px", "top": max_img_top+"px"});
				$("#dragContainer").append(process_str(array_info_tiles[array_min_top[i]].left, max_img_top, xy_new[0], xy_new[1], zoom_level));
				array_info_tiles[array_min_top[i]].left = array_info_tiles[array_min_top[i]].left;
				array_info_tiles[array_min_top[i]].top = max_img_top;
				array_info_tiles[array_min_top[i]].x = xy_new[0];
				array_info_tiles[array_min_top[i]].y = xy_new[1];
			}	
		}
		setTimeout('callback()', 500);
	}

	function zoom_map(event, zoom_in) {
		var scale;
		var delta_zoom;
		if(zoom_in) {
			scale = 2;
			delta_zoom = -1;
		} else {
			scale = 1/2;
			delta_zoom = 1;
		}
		var tile_x, tile_y;
		var delta_left_convert = $("#dragContainer").position().left - position_div_left_origin;
		var delta_top_convert = $("#dragContainer").position().top - position_div_top_origin
		var delta_left_click = (event.pageX - delta_left_convert) - left_show_default;  
		var delta_top_click = (event.pageY - delta_top_convert) - top_show_default;
		tile_x = x_default + Math.floor(delta_left_click/256);
		tile_y = y_default + Math.floor(delta_top_click/256);
		var x_pixel, y_pixel;
		if(delta_left_click < 0) {
			x_pixel = tile_x * 256 + (256 - Math.abs(delta_left_click%256));	
		} else {
			x_pixel = tile_x * 256 + Math.abs(delta_left_click%256);
		}
		if(delta_top_click < 0) {
			y_pixel = tile_y * 256 + (256 - Math.abs(delta_top_click%256));	
		} else {
			y_pixel = tile_y * 256 + Math.abs(delta_top_click%256);
		}		
		
		/*var test_convert_pixel_to_latlng = fromPixelToLatLng(x_pixel, y_pixel, zoom_level);
		alert("Latitude: " + test_convert_pixel_to_latlng.lat);
		alert("Longitude: " + test_convert_pixel_to_latlng.lng);

		var test_convert_latlng_to_pixel = fromLatLngToPixel(test_convert_pixel_to_latlng.lat, test_convert_pixel_to_latlng.lng, zoom_level);
		alert("pixel_x: " + x_pixel);
		alert("pixel_y: " + y_pixel);
		alert("pixel_x_convert: " + test_convert_latlng_to_pixel.x);
		alert("pixel_y_convert: " + test_convert_latlng_to_pixel.y);*/
		
		var tile_new_x = Math.floor((x_pixel * scale) / 256);
		var tile_new_y = Math.floor((y_pixel * scale) / 256);
		var delta_new_zoom_x = (x_pixel * scale) - (tile_new_x * 256) + delta_left_convert;		// do lech so voi goc toa do x anh moi
		var delta_new_zoom_y = (y_pixel * scale) - (tile_new_y * 256) + delta_top_convert;		// do lech so voi goc toa do y anh moi
		$('img').remove();
		zoom_level = zoom_level + delta_zoom;		
		array_info_tiles = init_map(tile_new_x, tile_new_y, (event.pageX - delta_new_zoom_x), (event.pageY - delta_new_zoom_y));		
		$("svg").remove();
	}

	function info_tile (left, top, x, y) {
	    this.left = left;
	    this.top = top;
	    this.x = x;
	    this.y = y;
	}

	function LatLng(lat, lng) {
		this.lat = lat;
		this.lng = lng;
	}
	function Pixel(x, y) {
		this.x = x;
		this.y = y;
	}
	function Coefficient(a, b) {
		this.a = a;
		this.b = b;
	}
	function point(id, lat, lng, info) {
		this.id = id;
		this.lat = lat;
		this.lng = lng;
		this.info = info;
	}

	function convert(x, add) {
		var result;
		result = x + add;
		return result;
	}

	function compute_number_img(wd_width, wd_height) {
		var result;
		var num_horizon = Math.round(wd_width/256);
		var num_vertical = Math.round(wd_height/256);
		result = num_horizon * num_vertical;
		return result;
	}
	
	function init_control() {
		$("#name_segment").val("");
		$("#sequence").val("");
		$("#txt_start_node").val("");
		$("#txt_end_node").val("");
		$("#longitude_locality").val("");
		$("#node_name").val("");
		$("#latitude_node").val("");
		$("#longitude_node").val("");
		$("#wayset").val("");
		$("#info_node").val("");
	}
	
	function init_map(x_init, y_init, left_show_default_var, top_show_default_var) {

		var array_info_tile = new Array(63);
		var count = 0;
		
		x_default = x_init;
		y_default = y_init;
		left_show_default = left_show_default_var;
		top_show_default = top_show_default_var;
		// top
		for(var i = -1; i > -5; i--) {
			for(var j = -3; j < 4; j++) {
				$("#dragContainer").append(process_str(left_show_default + (256 * i), top_show_default + (256 * j), x_init + i, y_init + j, zoom_level));
				var tile = new info_tile(left_show_default + (256 * i), top_show_default + (256 * j), x_init + i, y_init + j);
				array_info_tile[count] = tile;
				count++;
			}
		}  
		
		// center 
		for(var i = -3; i < 4; i++) {
			$("#dragContainer").append(process_str(left_show_default, top_show_default + (256 * i), x_init, y_init + i, zoom_level));
			var tile = new info_tile(left_show_default, top_show_default + (256 * i), x_init, y_init + i);
			array_info_tile[count] = tile;
			count++;
		}

		// bottom
		for(var i = 1; i < 5; i++) {
			for(var j = -3; j < 4; j++) {
				$("#dragContainer").append(process_str(left_show_default + (256 * i), top_show_default + (256 * j), x_init + i, y_init + j, zoom_level));
				var tile = new info_tile(left_show_default + (256 * i), top_show_default + (256 * j), x_init + i, y_init + j);
				array_info_tile[count] = tile;
				count++;
			}
		}
		return array_info_tile;
	}

	function process_str(left, top, x, y, z) {
		var default_str = "<img alt='Loading image...' style='position: absolute; top: " + top + "; left: " + left + "' src='./tiles/x=" + x + "&y=" + y + "&z=" + z + ".png'>" ;
		return default_str;
	}

	function process_str_src(x, y, z) {
		var default_str_src = "./tiles/x=" + x + "&y=" + y + "&z=" + z + ".png";
		return default_str_src;
	}

	function process_str_style(left, top) {
		var default_str_style = "position: absolute; top: " + top + "; left: " + left;
		return default_str_style;
	}

	function check_view(top_div, left_div, top_img, left_img, width_div, height_div, width_img, height_img) {
		var result;
	}

	function getXYfromSRC(src) {
		var arr_XY = new Array(2);
		var sub_src = src.substring(src.indexOf("?") + 1, src.length - 2) + "";	
		var sub_x = sub_src.substring(sub_src.indexOf("=") + 1, sub_src.indexOf("&")); 
		var sub_y = sub_src.substring(sub_src.lastIndexOf("=") + 1, sub_src.length);	
		arr_XY[0] = sub_x;
		arr_XY[1] = sub_y;
//		alert(sub_src);
		return arr_XY;
	}

	function check_min(array_ele) {
		var a = array_ele.length;
		var temp = array_ele[0];
		for(var i = 0; i < a; i++) {
			if(temp > array_ele[i]) {
				temp = array_ele[i];
			}
		}		
		return temp;
	}

	function check_max(array_ele) {
		var a = array_ele.length;
		var temp = array_ele[0];
		for(var i = 0; i < a; i++) {
			if(temp < array_ele[i]) {
				temp = array_ele[i];
			}
		}		
		return temp;
	}

	function getIndex(value, array) {
		var a = array.length;
		var result = new Array(7);
		var count = 0;
		for(var i = 0; i < a; i++) {			
			if(value == array[i]) {
				result[count] = i;
				count++;
			}
		}
		
		return result;
	}
	function convertXY(left_old, top_old, x_old, y_old, left_new, top_new) {
		var XY_new = new Array(2);
		XY_new[0] = (left_new - left_old) / 256 + x_old;
		XY_new[1] = (top_new - top_old) / 256 + y_old;
		return XY_new;
	}

	function fromLatLngToPixel(lat, lng, z) {
		// longitude -> x
		// latitude -> y
		var offset = 16777216;
		var radius = offset / Math.PI;

		var z_temp = Math.abs(z);
		var x;
		if(z < 0) {
			x = (offset+radius*(lng*Math.PI/180))<<z_temp;		
		} else {
			x = (offset+radius*(lng*Math.PI/180))>>z_temp;
		}
		var y;
		if(z < 0) {
			y = (offset-radius*Math.log((1+Math.sin(lat*Math.PI/180))/(1-Math.sin(lat*Math.PI/180)))/2)<<z_temp;
		} else {
			y = (offset-radius*Math.log((1+Math.sin(lat*Math.PI/180))/(1-Math.sin(lat*Math.PI/180)))/2)>>z_temp;
		}
		 
		var pixel = new Pixel(x, y);
		return pixel;
	}

	function fromPixelToLatLng(x, y, z) {
		var offset = 16777216;
		var radius = offset / Math.PI; 
		
		var temp1;
		var z_temp;
		if(z < 0) {
			z_temp = Math.abs(z);
			temp1 = ((y>>z_temp) - offset) / radius;
		} else {
			temp1 = ((y<<z) - offset) / radius;
		}
		
		var temp2 = 2 * Math.atan(Math.exp(temp1));
		var lat = ((Math.PI/2 - temp2) * 180) / Math.PI;
		
		var lng;		
		if(z < 0) {
			lng = ((((x>>z_temp) - offset) / radius) * 180) / Math.PI;
		} else {
			lng = ((((x<<z) - offset) / radius) * 180) / Math.PI;
		}
		
		
		var latlng = new LatLng(lat, lng);
		return latlng; 
	}

	function DecToDeg(d)
	{
		return d * Math.PI / 180.0;
	} 
	
	function round_f(num,ext){
		var tmp=Math.pow(10, ext);
		return (Math.round(num*tmp)/tmp);
	}
	
	function drawPath_Detail(array_node_of_way, zoom_level) {
		var start_node, end_node;
		var count = 0;
		var start_imagepixel_point = null;
		var end_imagepixel_point = null;
		var start_windowpixel_point = null;
		var end_windowpixel_point = null;
		$.each(array_node_of_way.way, function() {
			if(count > 0) {				
				end_node = new LatLng(this.lat, this.lng);
				
				// gtri pixel cua he qui chieu anh toan bo ban do cua diem dau diem cuoi
				start_imagepixel_point = fromLatLngToPixel(start_node.lat, start_node.lng, zoom_level);
				end_imagepixel_point = fromLatLngToPixel(end_node.lat, end_node.lng, zoom_level);
				
				// gtri pixel tinh theo he quy chieu man hinh cua diem dau diem cuoi
				start_windowpixel_point = convertPixelImageToPixelWindow(start_imagepixel_point.x, start_imagepixel_point.y);
				end_windowpixel_point = convertPixelImageToPixelWindow(end_imagepixel_point.x, end_imagepixel_point.y);
				
				// draw path
				drawPath(start_windowpixel_point, end_windowpixel_point, 5);
			}
			start_node = new LatLng(this.lat, this.lng);
			count++;
   		});
		moveMap_Pixel(start_windowpixel_point);
	}
	
	function drawBoundary(array_node_of_way, zoom_level) {
		var boundary_array = new Array();
		var node = null;
		var count = 0;
		$.each(array_node_of_way.way, function() {
			node = new LatLng(this.lat, this.lng);	
			node_LatLng = new LatLng(node.lat, node.lng);
				
			// gtri pixel cua he qui chieu anh toan bo ban do cua diem dau diem cuoi
			node_Pixel_Image = fromLatLngToPixel(node_LatLng.lat, node_LatLng.lng, zoom_level);
					
			// gtri pixel tinh theo he quy chieu man hinh cua diem dau diem cuoi
			node_Pixel_Window = convertPixelImageToPixelWindow(node_Pixel_Image.x, node_Pixel_Image.y);
			boundary_array[count] = node_Pixel_Window;
			count++;
		});
		
		// creat polygon boundary
		boundary_global = create_polygon_bound_from_way(boundary_array, zoom_level);
		// draw polygon boundary
		var start_node = null;
		var end_node = null;
		for(var i = 0; i < boundary_global.length; i++) {
			if(i > 0) {	
				end_node = boundary_global[i];
				drawPath(start_node, end_node, 3);
			}
			start_node = boundary_global[i];
			if(i == (boundary_global.length - 1)) {
				end_node = boundary_global[0];
				drawPath(start_node, end_node, 3);
			}
		}
		moveMap_Pixel(start_node);
	}
	
	function drawPath(start_node, end_node, stroke_width) {
		var x;
		var y;
		var width;
		var height;
		if(start_node.x < end_node.x) {
			x = start_node.x - stroke_width;
		} else {
			x = end_node.x - stroke_width;
		}
		if(start_node.y < end_node.y) {
			y = start_node.y - stroke_width;
		} else {
			y = end_node.y - stroke_width;
		}
		width = Math.abs((end_node.x - start_node.x)) + 10;
		height = Math.abs((end_node.y - start_node.y)) + 10;
		$("#svgContainer").append('<svg viewBox="' + x + ' ' + y + ' ' + width + ' ' + height + '" width="' + width + 'px" height="' + height + 'px" style="position: absolute; left: ' + x + 'px; top: ' + y + 'px; z-index: 1000;" overflow="visible" version="1.1"><path stroke-linejoin="round" stroke-linecap="round" d="M ' + start_node.x + ' ' + start_node.y + ' L ' + end_node.x + ' ' + end_node.y+ '" stroke="#0000ff" stroke-opacity="0.45" stroke-width="' + stroke_width + 'px" fill="none"></svg>');
	}
	
	function convertPixelImageToPixelWindow(pixel_image_x, pixel_image_y) {
		var pixel_window_x, pixel_window_y;
		var tile_x = Math.floor(pixel_image_x / 256);
		var tile_y = Math.floor(pixel_image_y / 256);
		var delta_pixel_x = pixel_image_x - (tile_x * 256);
		var delta_pixel_y = pixel_image_y - (tile_y * 256);
		
		var temp_x = (x_default - tile_x) * 256;
		var temp_y = (y_default - tile_y) * 256;
		var pixel_real_x = (left_show_default - temp_x) + delta_pixel_x;
		var pixel_real_y = (top_show_default - temp_y) + delta_pixel_y;
		var result = new Pixel(pixel_real_x, pixel_real_y);
		return result;
	}
	
	function setHeight_Page() {
		var height_page = $(window).height() - $("#div_item").height();
		$("#div_content").css({"height": + height_page + "px"});
		$("#tileContainer").css({"height": + height_page + "px"});
		setTimeout('setHeight_Page()', 100);
	}
	
	function moveMap_LatLng(node, z) {
		var delta_x = node.x - ($("#tileContainer").width()/2);
		var delta_y = node.y - ($("#tileContainer").height()/2);
		if(delta_x > 0) {
			delta_x = -delta_x;
		} 
		if(delta_y > 0) {
			delta_y = -delta_y;
		}
		$("#dragContainer").css({"left": + delta_x + "px", "top": delta_y + "px"});
		/*var pixel_point = fromLatLngToPixel(node.lat, node.lng, z);
		var tile_x = Math.floor(pixel_point.x / 256);
		var tile_y = Math.floor(pixel_point.y / 256);
		var delta_pixel_x = pixel_point.x - (tile_x * 256);
		var delta_pixel_y = pixel_point.y - (tile_y * 256);		
//		alert("tile_x = " + tile_x + "tile_y = " + tile_y);
//		alert($(window).height());
//		alert($("#tileContainer").height());
		left_show_default = $("#tileContainer").width()/2 - ((256/2) + delta_pixel_x);
		top_show_default = $("#tileContainer").height()/2 - ((256/2) + delta_pixel_y);
		//alert("left_show_default = " + left_show_default + "top_show_default = " + top_show_default);
		$("img").remove();
//		$("#dragContainer").css({"left": + 0 + "px", "top": 0 + "px"});
		array_info_tiles = init_map(tile_x, tile_y, left_show_default, top_show_default);*/
	}
	
	function moveMap_Pixel(node) {
		
		var x_dragcomparetile, y_dragcomparetile;
		var x_pointcomparetile, y_pointcomparetile;
		var x_pointcomparedrag, y_pointcomparedrag;
		
		x_pointcomparedrag = node.x;
		y_pointcomparedrag = node.y;
		
		x_pointcomparetile = Math.floor($("#tileContainer").width()/2);
		y_pointcomparetile = Math.floor($("#tileContainer").height()/2);
		
		x_dragcomparetile = x_pointcomparetile - x_pointcomparedrag;
		y_dragcomparetile = y_pointcomparetile - y_pointcomparedrag;

		$("#dragContainer").css({"left": + x_dragcomparetile + "px", "top": y_dragcomparetile + "px"});
	}
	
	function callback_getResponse() {
		var start_node = null;
		var end_node = null;
		var start_imagepixel_point = null;
		var end_imagepixel_point = null;
		var start_windowpixel_point = null;
		var end_windowpixel_point = null;
		if(data_response != null) {
			// gtri lat lng cua diem dau diem cuoi
			start_node = new LatLng(data_response.start_node.lat, data_response.start_node.lng);
			end_node = new LatLng(data_response.end_node.lat, data_response.end_node.lng);
			
			// gtri pixel cua he qui chieu anh toan bo ban do cua diem dau diem cuoi
			start_imagepixel_point = fromLatLngToPixel(start_node.lat, start_node.lng, zoom_level);
			end_imagepixel_point = fromLatLngToPixel(end_node.lat, end_node.lng, zoom_level);
			
			// gtri pixel tinh theo he quy chieu man hinh cua diem dau diem cuoi
			start_windowpixel_point = convertPixelImageToPixelWindow(start_imagepixel_point.x, start_imagepixel_point.y, zoom_level);
			end_windowpixel_point = convertPixelImageToPixelWindow(end_imagepixel_point.x, end_imagepixel_point.y, zoom_level);
			
			// draw path
			$("svg").remove();
			drawPath(start_windowpixel_point, end_windowpixel_point,5);
			//moveMap_LatLng(start_node.lat, start_node.lng, zoom_level);
		} else {
			setTimeout('callback_getResponse()', 100);
		}
	}
	
	function check_part_polygon(array_node_of_way) {
		var num_part = 0;
		var start_node, end_node;
		var count = 0;
		$.each(array_node_of_way.way, function() {
			if(count == 0) {
				start_node = new LatLng(this.lat, this.lng);
			}
			end_node = new LatLng(this.lat, this.lng);
		});
		
		return num_part;
	}
	
	function insert_img_node(type_node, lat, lng, zoom_level) {
		var str = '';
		// gtri pixel cua he qui chieu anh toan bo ban do cua diem dau diem cuoi
		imagepixel_node = fromLatLngToPixel(lat, lng, zoom_level);
		
		// gtri pixel tinh theo he quy chieu man hinh cua diem dau diem cuoi
		windowpixel_point = convertPixelImageToPixelWindow(imagepixel_node.x, imagepixel_node.y);
		
		var left = windowpixel_point.x - 10;
		var top = windowpixel_point.y - 34;
		if(type_node == 0) {
			str = "<img overflow='visible' style='z-index:1000; position: absolute; left: " + left + "; top: " + top + "' src='images/marker_greenA.png'/>" ;
		} else {
			str = "<img overflow='visible' style='z-index:1000; position: absolute; left: " + left + "; top: " + top + "' src='images/marker_greenB.png'/>" ;
		}
		$("#dragContainer").append(str);
	}
	
	function InPolygon(node_input, polygon, n) {
		var i, i1;
		var d;
		var x;
		var Rcross = 0;
		var Lcross = 0;
		var Rstrad, Lstrad;
		var boundary_global_temp = new Array();
		var node_temp = null;
		
		for(i = 0; i < n; i++) {
			var x = polygon[i].x - node_input.x;
			var y = polygon[i].y - node_input.y;
			node_temp = new Pixel(x, y);
			boundary_global_temp[i] = node_temp;
		}
		
		for(i = 0; i < n; i++) {
			if(boundary_global_temp[i].x == 0 && boundary_global_temp[i].y == 0)	return 'v';
			i1 = (i + n - 1) % n;
			
			Rstrad = (boundary_global_temp[i].y > 0) != (boundary_global_temp[i1].y > 0);
			Lstrad = (boundary_global_temp[i].y < 0) != (boundary_global_temp[i1].y < 0);
			
			if(Rstrad || Lstrad) {
				x = (boundary_global_temp[i].x * boundary_global_temp[i1].y - boundary_global_temp[i1].x * boundary_global_temp[i].y) / (boundary_global_temp[i1].y - boundary_global_temp[i].y);
				
				if(Rstrad && x > 0)	Rcross++;
				if(Rstrad && x < 0)	Lcross++;
			}
		}
		
		if((Rcross % 2) != (Lcross % 2))	return 'e';
		
		if((Rcross % 2) == 1)	return 'i';
		else	return 'o';		
	}
	
	function create_polygon_bound_from_way(array_node_of_way, zoom_level) {
		var boundary = new Array();
		var distance = 10>>zoom_level;
		var node_LatLng = null;
		var node_Pixel_Image = null;
		var node_Pixel_Window = null;
		var node_Pixel_Window_top = null;
		var node_Pixel_Window_botttom = null;
		var f_x = null;
		var f_prev_x = null;
		var f1_x = null;
		var f2_x = null;
		var f4_x = null;
		var f_middle_x = null;
		var start_intersection = null;
		var middle_intersection = null;
		var end_intersection = null;
		var x_intersection;
		var y_intersection;
		var is_type_special_prev = -1;
		var x_direction, y_direction; // quy uoc doi voi x: 0 la tu duoi --> tren; 1 tu tren --> duoi	
									  // ------- doi voi y: 0 la tu trai --> phai; 1 tu phai --> trai				
		var count = 0;
		for(var i = 0; i < array_node_of_way.length; i++) {
			if(i > 0) {					
				end_node = array_node_of_way[i];	
				// X_SPECIAL
				if(((start_node.x - end_node.x) < 2 && (start_node.x - end_node.x) > -2)) {
					if(((start_node.y - end_node.y) > 2 && (start_node.y - end_node.y) < -2)) {
						f_x = start_node.x;
						if(i == 1) {							
							x_intersection = start_node.x + distance;
							y_intersection = start_node.y;		
							start_intersection = new Pixel(x_intersection, y_intersection);
							boundary[count] = start_intersection;
							count++;
							f_prev_x = x_intersection;
						} 
						if(i < (array_node_of_way.length - 1) && i > 1) {
							if(is_type_special_prev == 0) {
								x_intersection = f_prev_x;
								y_intersection = start_node.y;		
								middle_intersection = new Pixel(x_intersection, y_intersection);
								boundary[count] = middle_intersection;
								count++;			
								f_prev_x = x_intersection;
							} else if(is_type_special_prev == 1) {
								alert(f_prev_x);
								if(y_direction == 0) {
									if(start_node.y < end_node.y) {
										x_intersection = start_node.x + (-distance);
									} else {
										x_intersection = start_node.x + distance;
									}
								} else {
									if(start_node.y < end_node.y) {
										x_intersection = start_node.x + distance;
									} else {
										x_intersection = start_node.x + (-distance);
									}
								}
								y_intersection = f_prev_x;	
								middle_intersection = new Pixel(x_intersection, y_intersection);
								boundary[count] = middle_intersection;
								count++;
								f_prev_x = x_intersection;
							} else {
								alert(f_prev_x);
								if(f_prev_x.a > 0) {
									if(start_node.y < end_node.y) {
										x_intersection = start_node.x + (-distance);		
									} else {
										x_intersection = start_node.x + distance;
									}
								} else {
									if(start_node.x < end_node.x) {
										x_intersection = start_node.x + distance;		
									} else {
										x_intersection = start_node.x + (-distance);
									}
								}
								y_intersection = f_prev_x.a * x_intersection + f_prev_x.b;
								middle_intersection = new Pixel(x_intersection, y_intersection);
								boundary[count] = middle_intersection;
								count++;
								f_prev_x = x_intersection;
							}						
						} 
						if(i == (array_node_of_way.length - 1)){
							x_intersection = end_node.x + distance;
							y_intersection = end_node.y;
							end_intersection = new Pixel(x_intersection, y_intersection);
							boundary[count] = end_intersection;
							count++;
						}
						is_type_special_prev = 0;
						if(start_node.y - end_node.y) {
							x_direction = 0;	// duoi --> tren
						} else {
							x_direction = 1;	// tren --> duoi
						}
					}
				} else if(((start_node.y - end_node.y) < 2 && (start_node.y - end_node.y) > -2)) {		// Y_SPECIAL
					if(((start_node.x - end_node.x) > 2 && (start_node.x - end_node.x) < -2)) {
						f_x = start_node.y;
						if(i == 1) {
							x_intersection = start_node.x;
							y_intersection = start_node.y + distance;		
							start_intersection = new Pixel(x_intersection, y_intersection);
							boundary[count] = start_intersection;
							count++;
							f_prev_x = y_intersection;
						} 
						if(i < (array_node_of_way.length - 1) && i > 1) {
							if(is_type_special_prev == 0) {
								x_intersection = f_prev_x;	
								if(x_direction == 0) {
									if(start_node.y < end_node.y) {
										y_intersection = start_node.y + (-distance);	
									} else {
										y_intersection = start_node.y + distance;	
									}
								} else {
									if(start_node.y < end_node.y) {
										y_intersection = start_node.y + distance;	
									} else {
										y_intersection = start_node.y + (-distance);	
									}
								}													
								middle_intersection = new Pixel(x_intersection, y_intersection);
								boundary[count] = middle_intersection;
								count++;
								f_prev_x = y_intersection;
							} else if(is_type_special_prev == 1) {
								x_intersection = start_node.x;
								y_intersection = f_prev_x;		
								//alert("x_intersection = " + x_intersection + "; y_intersection = " + y_intersection);
								middle_intersection = new Pixel(x_intersection, y_intersection);
								boundary[count] = middle_intersection;
								count++;			
								f_prev_x = y_intersection;
							} else {		
								if(f_prev_x.a > 0) {
									alert("dong bien");
									if(start_node.x < end_node.x) {
										y_intersection = start_node.y + distance;							
									} else {
										y_intersection = start_node.y + (-distance);
									}
								} else {
									alert("nghich bien");
									if(start_node.x < end_node.x) {
										y_intersection = start_node.y + (-distance);			
									} else {
										y_intersection = start_node.y + distance;
									}
								}
								x_intersection = (y_intersection - f_prev_x.b) / f_prev_x.a;						
								//alert("x_intersection = " + x_intersection + "; y_intersection = " + y_intersection);
								middle_intersection = new Pixel(x_intersection, y_intersection);
								boundary[count] = middle_intersection;
								count++;
								f_prev_x = y_intersection;
							}
						} 
						if(i == (array_node_of_way.length - 1)){
							x_intersection = end_node.x;
							y_intersection = end_node.y + distance;
							end_intersection = new Pixel(x_intersection, y_intersection);
							boundary[count] = end_intersection;
							count++;
						}
						is_type_special_prev = 1;
						if(start_node.x - end_node.x) {
							y_direction = 0;	// trai --> phai
						} else {
							y_direction = 1;	// phai --> trai
						}
					}
				} else {			// NOT_SPECIAL
					f_x = F_x(start_node, end_node);
					if(i == 1) {
						f_prev_x = find_parallel(f_x, distance);	// duong thang song song duong	
						//alert("f_prev_x.a = " + f_prev_x.a + "; f_prev_x.b = " + f_prev_x.b);
						f1_x = find_perpendicular(f_x, start_node);			
						//alert("f1_x.a = " + f1_x.a + "; f1_x.b = " + f1_x.b);
						
						start_intersection = find_intersection(f1_x, f_prev_x);
						//alert("start_intersection.x = " + start_intersection.x + "; start_intersection.y = " + start_intersection.y);
						boundary[count] = start_intersection;
						count++;
					}
					
					is_type_special_prev = 2;
				}				
			}
			start_node = array_node_of_way[i];
		}		
		return boundary;
	}
	
	function F_x(start_node, end_node) { 	
		
		//alert("start_node.x = " + start_node.x + "; start_node.y = " + start_node.y);
		//alert("end_node.x = " + end_node.x + "; f_xend_node.y = " + end_node.y);
		var a = (start_node.y - end_node.y) / (start_node.x - end_node.x);
		
		var b1 = (start_node.x * end_node.y) - (end_node.x * start_node.y);
		var b2 = start_node.x - end_node.x;		
		var b = b1 / b2;
		
		//alert("a = " + a + "; b = " + b);
		
		var result = new Coefficient(a, b);
		return result;
	}
	
	function check_special(start_node, end_node) {
		var result;
		if(start_node.x == end_node.x) {
			result = start_node.x;
		}
		if(start_node.y == end_node.y) {
			result = start_node.y;
		}
		return result;
	}
	
	function find_parallel(f_x, distance) {
		var a = f_x.a;
		var b = f_x.b + distance;
		
		var result = new Coefficient(a, b);
		return result;
	}
	
	function find_perpendicular(f_x, node) {
		var a = -f_x.a;
		var b = node.y + f_x.a * node.x;
		
		var result = new Coefficient(a, b);
		return result;
	}
	
	function find_intersection(f1_x, f2_x) {
		var x = (f2_x.b - f1_x.b) / (f1_x.a - f2_x.a);
		
		var y1 = (f1_x.a * f2_x.b) - (f2_x.a * f1_x.b);
		var y2 = f1_x.a - f2_x.a;
		var y = y1 / y2;
		
		var result = new Pixel(x, y);
		return result;
	}
	
	function time_process(time) {
		var hour = Math.floor(time/3600);
        hour = ((hour < 10) ? "0" : "") + hour;
        var minute = Math.floor((time - hour*3600)/60);
        minute = ((minute < 10) ? "0" : "") + minute;
        var second = time-(hour*3600 + minute*60);
        second = ((second < 10) ? "0" : "") + second; 
        document.getElementById("timedisplay").innerHTML = hour + ":" + minute + ":" + second;
	}
	
	function create_graph() {
	
	}
	
	function cal_real_distance(array_way) {
		var dis = 0;
		var count = 0;
		$.each(array_way.way, function() {
			if(count > 0) {				
				end_node = new LatLng(this.lat, this.lng);
				dis += cal_distance(start_node, end_node)
			}
			start_node = new LatLng(this.lat, this.lng);
			count++;
   		});
		return dis;
	}
	
	function cal_distance(start_point, end_point) {
		var R = 6371; // km
		var dLat = (end_point.lat - start_point.lat)*Math.PI/180;
		var dLon = (end_point.lng - start_point.lng)*Math.PI/180;
		var lat1 = start_point.lat*Math.PI/180;
		var lat2 = end_point.lat*Math.PI/180;
		
		var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
				Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2); 
		var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
		var d = R * c;
		return d;
	}
	
	/*function NodeDB(id, nodeTo) {
		this.id = id;
		this.nodeTo = nodetTo;
	}
	
	function insert_nodeTo(array_node, array_segment) {
		var json = '{"node":[';
		$.each(array_node.node, function() {
			var id = this.id;
			var nodeTo = '';
			$.each(array_segment.segment, function() {
				if(this.segment_id = id) {
					var temp = this.end_node + ", "; 
					nodeTo += temp;
				}
			});
			nodeTo = nodeTo.substring(0, nodeTo.lastIndexOf(","));	
			var str_temp = '{"id":"' + id + '","nodeTo":"' + nodetTo + '"}';
			json += str_temp;
			{"way":[{"id":"75619049","lat":"21.032309","lng":"105.851891"},{"id":"81794511","lat":"21.033084","lng":"105.851942"},{"id":"322505299","lat":"21.03351","lng":"105.852024"},{"id":"84782074","lat":"21.033915","lng":"105.85204"}]}
		});		
	}*/
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	